#include <standard/system.h>
#include "sceneobjectremovecmd.h"
#include "editorscenemgr.h"
#include "scene/editorscene.h"
#include "scene/sceneobjectnavigationmap.h"
#include "sceneobjects/sceneobjectmanager.h"
#include "sceneobjects/sceneobject.h"



CSceneObjectRemoveCMD::CSceneObjectRemoveCMD( list<uint32> *pvRemoveInfoList ):
CCommand(){
    for( list<uint32>::iterator it = pvRemoveInfoList->begin();
         it != pvRemoveInfoList->end(); it ++ ){
        SSceneObjectRemoveInfo _sSORemoveInfo;
        _sSORemoveInfo.m_nID = *it;
        _sSORemoveInfo.m_strFileName[0] = 0;
        _sSORemoveInfo.m_sPos = {0};
        _sSORemoveInfo.m_sRot = {0};
        _sSORemoveInfo.m_sScale = {0};
        _sSORemoveInfo.m_sMapPos = {0};
        m_vRemoveInfoList.push_back( _sSORemoveInfo );
    }
}
CSceneObjectRemoveCMD::~CSceneObjectRemoveCMD(){

}
void CSceneObjectRemoveCMD::Init(){
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    for( list<SSceneObjectRemoveInfo>::iterator it = m_vRemoveInfoList.begin();
         it != m_vRemoveInfoList.end(); it ++){
        SSceneObjectRemoveInfo* _pSORemoveInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSORemoveInfo->m_nID );
        if( _pSO ){
            strcpy( _pSORemoveInfo->m_strName, _pSO->m_strName );
            strcpy( _pSORemoveInfo->m_strFileName, _pSO->m_strFileName );
            _pSORemoveInfo->m_fInitScale = _pSO->m_fInitScale;
            _pSORemoveInfo->m_fHelperRadius = _pSO->m_fHelperRadius;
            _pSORemoveInfo->m_nCellType = _pSO->m_nCellType;
            _pSORemoveInfo->m_nRotationType = _pSO->m_nRotationType;
            _pSORemoveInfo->m_eSceneObjectType = _pSO->m_eSceneObjectType;
            float* _pPos = _pSO->m_vPosition;
            float* _pRot = _pSO->m_vRotation;
            float* _pSca = _pSO->m_vScale;
            TV3_SETS( _pSORemoveInfo->m_sPos, _pPos[0], _pPos[1], _pPos[2]);
            TV3_SETS( _pSORemoveInfo->m_sRot, _pRot[0], _pRot[1], _pRot[2]);
            TV3_SETS( _pSORemoveInfo->m_sScale, _pSca[0], _pSca[1], _pSca[2]);
            _pSORemoveInfo->m_sMapPos = _pSO->m_sMapGridPos;

            _pSORemoveInfo->m_strExtData = NewString( _pSO->m_strExtData.c_str() );
        }
    }
    Redo();
}
void CSceneObjectRemoveCMD::Final(){
    for( list<SSceneObjectRemoveInfo>::iterator it = m_vRemoveInfoList.begin();
         it != m_vRemoveInfoList.end(); it ++){
        SSceneObjectRemoveInfo* _pSORemoveInfo = &(*it);
        FREE( _pSORemoveInfo->m_strExtData );
    }
    m_vRemoveInfoList.clear();
}
bool32 CSceneObjectRemoveCMD::Undo(){
    CSceneObjectNavigationMap* _pSceneObjectNavigationMap = m_pSceneMgr->GetSceneObjectNavigationMap();
    CMDXParticleManager* _pParticleMgr = m_pSceneMgr->GetScene()->GetParticleManager();
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();

    for( list<SSceneObjectRemoveInfo>::iterator it = m_vRemoveInfoList.begin();
         it != m_vRemoveInfoList.end(); it ++){
        SSceneObjectRemoveInfo* _pSORemoveInfo = &(*it);
        CSceneObject* _pSceneObject = _pSceneObjectManager->AddSceneObject( _pSORemoveInfo->m_nID, 
                                              _pSORemoveInfo->m_strFileName,
                                              _pSORemoveInfo->m_strName,
                                              _pSORemoveInfo->m_eSceneObjectType,
                                              _pSORemoveInfo->m_fInitScale,
                                              _pSORemoveInfo->m_fHelperRadius,
                                              _pSORemoveInfo->m_nCellType,
                                              _pSORemoveInfo->m_nRotationType,
                                              _pSORemoveInfo->m_strExtData,
                                              &_pSORemoveInfo->m_sPos.X, 
                                              &_pSORemoveInfo->m_sRot.X, 
                                              &_pSORemoveInfo->m_sScale.X,
                                              _pSORemoveInfo->m_sMapPos.X,
                                              _pSORemoveInfo->m_sMapPos.Y );

        list<TPosition >_vNavigPositionList;
        _pSceneObjectNavigationMap->GetCellTypeListValue( _pSORemoveInfo->m_nCellType, 0, _pSORemoveInfo->m_sMapPos.X, _pSORemoveInfo->m_sMapPos.Y, &_vNavigPositionList );
        _pSceneObjectNavigationMap->SetSOListValue( &_vNavigPositionList, _pSceneObject );
    }
    return True;
}
bool32 CSceneObjectRemoveCMD::Redo(){
    CSceneObjectNavigationMap* _pSceneObjectNavigationMap = m_pSceneMgr->GetSceneObjectNavigationMap();
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();

    for( list<SSceneObjectRemoveInfo>::iterator it = m_vRemoveInfoList.begin();
         it != m_vRemoveInfoList.end(); it ++){
        SSceneObjectRemoveInfo* _pSORemoveInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSORemoveInfo->m_nID );
        if( _pSO ){
            list<TPosition> _vPathList;
            _pSceneObjectNavigationMap->GetCellTypeListValue( _pSO->m_nCellType, 0, _pSO->m_sMapGridPos.X, _pSO->m_sMapGridPos.Y, &_vPathList );
            _pSceneObjectNavigationMap->SetSOListValue( &_vPathList, NULL );
            _pSceneObjectManager->RemoveSceneObject( _pSORemoveInfo->m_nID );
        }
    }
    return True;
}